package edu.xjdx.os.controller;

import edu.xjdx.os.domain.*;
import edu.xjdx.os.domain.Dictionary;
import edu.xjdx.os.service.*;
import edu.xjdx.os.utils.DateUtil;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DictionaryService dictionaryService;
    @RequestMapping("/list")
    public String list(Model model){
        List<Order> orders=orderService.findAll();
        for (Order order:orders){
            order.setOrderStatus(dictionaryService.findById(order.getOrderStatus()).getDictionaryValue());

        }
        model.addAttribute("orders",orders);
        List<Dictionary> dictionaries = dictionaryService.findByName("订单状态");
        model.addAttribute("orderStatus",dictionaries);
        return "/back/order_list";
    }

    @GetMapping("/listAll")
    public String listAll(){
        return "/fore/myorder_list";
    }

    @RequestMapping("/listorder")
    @ResponseBody
    public List<Order> listOrders(HttpSession session){
        String id=(String) session.getAttribute("customerId");
        Customer customer = customerService.findById(id);
        List<Order> orders = orderService.findByCustomer(customer.getCustomerId());
        for (Order order:orders){
            order.setOrderStatus(dictionaryService.findById(order.getOrderStatus()).getDictionaryValue());
        }
        return orders;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object del(String id){
        orderService.remove(id);
        Map<String,Object> map=new HashMap<>();
        map.put("ok",true);
        return map;
    }


    @PostMapping("/modify")
    public String modify(Order order,@RequestParam("ordersTime") String ordersTime){
        Dictionary dictionary = dictionaryService.findByValue(order.getOrderStatus());
        Date date = new Date();
        date = DateUtil.parseStrToDate(ordersTime,DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS,date);
        order.setOrderTime(date);
        order.setOrderStatus(dictionary.getDictionaryId());
        orderService.modify(order);
        return "redirect:/order/list";
    }

    @PostMapping("/foremodify")
    public String foremodify(Order order,@RequestParam("ordersTime") String orderTime){
        Dictionary dictionary = dictionaryService.findByValue(order.getOrderStatus());
        Date date = new Date();
        date = DateUtil.parseStrToDate(orderTime,DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS,date);
        order.setOrderTime(date);
        order.setOrderStatus(dictionary.getDictionaryId());
        orderService.modify(order);
        return "redirect:/order/listAll";
    }


    @RequestMapping("/od")
    @ResponseBody
    public List<OrderDetailShow> orderWithGoods(@RequestParam("id")String id){
        List<OrderDetailShow> orderDetailShows = new ArrayList<>();
        Order order = orderService.findById(id);
        List<OrderDetailShow> orderDetailShows1 = orderDetailService.findByOrder(order);
        for (OrderDetailShow orderDetailShow : orderDetailShows1){
            if (orderDetailShow != null) {
                orderDetailShows.add(orderDetailShow);
            }
        }
        return orderDetailShows;
    }
}
